<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="发起讨论议题"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="名称" labelAlign="left" label-width="190" prop="issue">
					<u-input v-model="form.issue" type="text" placeholder="请输入名称" />
				</u-form-item>
				<u-form-item class="form-item" required label="讨论组" labelAlign="left" label-width="190" prop="groupId">
					<u-input v-model="groupId" type="select" :select-open="groupShow" placeholder="请选择讨论组" @click="groupShow = true" />
					<u-select :list="groupList" v-model="groupShow" @confirm="groupConfirm"></u-select>
				</u-form-item>

				<u-form-item class="form-item" required label="截止时间" labelAlign="left" label-width="190" prop="deadlineDate">
					<date-time-picker placeholder="请选择截止时间" fields="day" @change="timeChange($event, 'deadlineDate')"></date-time-picker>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">发布</u-button></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			groupId: '',
			groupShow: false,
			groupList: [],

			form: {
				issue: '',
				deadlineDate: '',
				groupId: '',
				clientCode: '',
				createBy: ''
			},
			rules: {
				issue: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				groupId: [
					{
						required: true,
						message: '请选择讨论组',
						trigger: 'change'
					}
				],
				deadlineDate: [
					{
						required: true,
						message: '请选择截止时间',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadGroup();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		groupConfirm(e) {
			this.groupId = e[0].label;
			this.form.groupId = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.sysUser) {
			} else {
				this.$logout();
			}
		},
		loadGroup() {
			this.$request('/wlyz/group/list_publish', {
				clientCode: this.userInfo.sysUser.clientCode,
				currentUserId: this.userInfo.sysUser.id,
				wlyzGroup: {
					status: 1
				}
			}).then(res => {
				this.groupList = [];
				res.data.forEach(item => {
					this.groupList.push({
						value: item.id,
						label: item.groupName
					});
				});
			});
		},
		confirm() {
			this.$refs.uForm.validate(valid => {
				let data = {};
				this.form.clientCode = this.userInfo.sysUser.clientCode;
				this.form.createBy = this.userInfo.sysUser.id;

				data = {
					wlyzIssue: this.form,
					glyIds: '',
					zyIds: ''
				};
				this.$request('/wlyz/issue/add', data, {
					load: true
				}).then(res => {
					this.$msg(res.msg);
					setTimeout(() => {
						let pre = this.$prePage();
						if (pre && pre.loadData) {
							pre.loadData('refresh');
						}
						uni.navigateBack();
					}, 500);
				});
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		display: block;
		padding: 0 30rpx;
		border-bottom: 20rpx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
